package com.ruoyi.web.controller.rest.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserShare;
import com.ruoyi.system.service.IWebUserService;
import com.ruoyi.system.service.IWebUserShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 服务预约订单Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-23
 */
@Api(tags = "用户分享")
@RestController
@RequestMapping("/rest/user/share")
public class _WebUserShareController extends BaseController
{

//    @Autowired
//    private IWebServerOrderService webServerOrderService;
    @Autowired
    private IWebUserService iWebUserService;
    @Autowired
    private IWebUserShareService iWebUserShareService;

    @ApiOperation("用户分享列表")
    @GetMapping("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
    })
    public AjaxResult list(Long userId)
    {
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        List<Map<String,Object>> maps = iWebUserShareService.listMaps(new QueryWrapper<WebUserShare>()
                .eq("user_id",userId)
                .select("id","create_time","content","comment","img1","img2")
        );

        return AjaxResult.success(maps);
    }

    @ApiOperation("用户分享详情")
    @GetMapping("detail/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "shareId", value = "分享数据id", required = true),
    })
    public AjaxResult list(Long userId , @PathVariable("id") Long shareId)
    {
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        WebUserShare webUserShare = iWebUserShareService.getById(shareId);

        return AjaxResult.success(webUserShare);
    }

    @ApiOperation(value = "保存",notes = "content:效果感言,comment:提升意见,img1:图片1,img2:图片2")
    @PostMapping("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
    })
    public AjaxResult save(Long userId,String content,String comment,String img1,String img2)
    {
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        WebUserShare webUserShare = new WebUserShare();
        webUserShare.setUserId(userId);
        webUserShare.setDeptId(userData.getDeptId());
        webUserShare.setComment(comment);
        webUserShare.setContent(content);
        webUserShare.setImg1(img1);
        webUserShare.setImg2(img2);
        return AjaxResult.success(iWebUserShareService.save(webUserShare));
    }

}
